package com.lgy.httpmocktool.control;

import com.alibaba.fastjson.JSONObject;
import com.lgy.httpmocktool.dao.bean.GroovyRespJson;
import com.lgy.httpmocktool.dao.GroovyRespJsonRepository;
import com.lgy.httpmocktool.util.GroovyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

@RestController
@RequestMapping("/client")
@Slf4j
public class MainControl {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private GroovyRespJsonRepository groovyRespJsonRepository;
    @PostMapping(value = "/mock.post")
    public JSONObject mock(@RequestBody JSONObject reqBody) {
        log.info("mock成功接受到请求报文");
        JSONObject respBody = GroovyUtil.executeDefault(GroovyUtil.getGroovyName(reqBody), reqBody);
        log.info("mock成功返回响应报文");
        return respBody;
    }
    @GetMapping(value = "/jpa.get")
    public void  jpa() {
        List<GroovyRespJson> all = groovyRespJsonRepository.findAll();
        all.forEach(System.out::println);

        GroovyRespJson one = groovyRespJsonRepository.findByRespKeyAndRespGroup("1","test.interface");
        System.out.println(one);

    }



}
